//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <buola/buola.h>

#include <buola/algorithm/container.h>
#include <buola/io/cfstab.h>

#if BUOLA_PLATFORM_LINUX
#include <mntent.h>
#else //BUOLA_PLATFORM_LINUX
#include <fstab.h>
#endif //BUOLA_PLATFORM_LINUX

namespace buola { namespace io {

bool CFSTab::mInit=false;
std::vector<std::unique_ptr<CFSTab::SEntry>> CFSTab::mEntries;

const CFSTab::SEntry *CFSTab::GetForDevice(const std::string &pDevice)
{
    if(!mInit) Read();

    for(int i=0;i<mEntries.size();i++)
    {
        if(mEntries[i]->mDevice==pDevice)
            return mEntries[i].get();
    }

    return nullptr;
}

const CFSTab::SEntry *CFSTab::GetForMountPoint(const std::string &pMountPoint)
{
    if(!mInit) Read();

    for(int i=0;i<mEntries.size();i++)
    {
        if(mEntries[i]->mMountPoint==pMountPoint)
            return mEntries[i].get();
    }

    return nullptr;
}

#if BUOLA_PLATFORM_LINUX
void CFSTab::Read()
{
    FILE *lFP=setmntent("/etc/fstab","r");

    char lBuffer[1024];
    struct mntent lE;

    while(getmntent_r(lFP,&lE,lBuffer,1024))
    {
        SEntry &lNew=construct_back(mEntries);

        lNew.mDevice=lE.mnt_fsname;
        lNew.mMountPoint=lE.mnt_dir;
        lNew.mType=lE.mnt_type;
        lNew.mOptions=lE.mnt_opts;
        lNew.mFrequency=lE.mnt_freq;
        lNew.mPassNo=lE.mnt_passno;
    }

    endmntent(lFP);

    mInit=true;
}
#else //BUOLA_PLATFORM_LINUX
void CFSTab::Read()
{
    setfsent();

    while(struct fstab *lE=getfsent())
    {
        SEntry &lNew=construct_back(mEntries);
        
        lNew.mDevice=lE->fs_spec;
        lNew.mMountPoint=lE->fs_file;
        lNew.mType=lE->fs_type;
        lNew.mOptions=lE->fs_mntops;
        lNew.mFrequency=lE->fs_freq;
        lNew.mPassNo=lE->fs_passno;
    }

    endfsent();

    mInit=true;
}
#endif //BUOLA_PLATFORM_LINUX

/*namespace io*/ } /*namespace buola*/ }
